package com.tongji.seller.model
{
	import com.adobe.serialization.json.JSON;
	import com.tongji.seller.events.GetStoreFavoriteStateEvent;
	import com.tongji.seller.events.GetStoreRecentOperateSceneStateEvent;
	import com.tongji.seller.events.GetStoreSceneDetailEvent;
	import com.tongji.seller.events.GetStoreSystemSceneHotStateEvent;
	import com.tongji.seller.events.GetStoreSystemSceneStateEvent;
	import com.tongji.seller.events.utilsEvent.PageChangeEvent;
	import com.tongji.seller.utils.PageTools;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 店铺版系统场景信息，店铺搭配的用户场景，用户保存的系统场景
	 * @author chenzhongyuan
	 * 2011-12-6
	 */	
	[Bindable]
	public class StoreSceneModel extends Actor
	{	
		/**
		 * 0:所有场景
		 * 1:搜索
		 * 2:热门场景
		 * 3:我的收藏
		 * 4:最近使用 
		 */		
		public var stateVar:int = 2;
		public var cdIdStr:String;
		public var fgIdStr:String;
		
		public var selectedSceneArray:ArrayCollection = new ArrayCollection(); //用户选择过的系统场景
		public var systemSceneArray:ArrayCollection; //绑定至第一步中的系统场景，包括搜索、热门、收藏、最近使用的场景
		public var matchSceneArray:ArrayCollection;  //用户保存的新的用户场景

		public var savedSceneCount:String;
		
		public var sceneCount:Number;

		public var matcharray:ArrayCollection;
		
		public var sceneDetail:Object; //场景的详细信息
		
		public var scene:String;
		public var sceneArr:ArrayCollection;	
		public var storeSystemSceneArray:ArrayCollection;
		public var topHotSceneArray:ArrayCollection; //前12个热门场景
		public var hotSceneArray:ArrayCollection;
		public var favoriteSceneArray:ArrayCollection;
		public var recentSceneArray:ArrayCollection;
		public var favoriteStateSceneArray:ArrayCollection;
		public var systemStateSceneArray:ArrayCollection;
		public var RecentOperateSceneStateArray:ArrayCollection;
		public var systemStateHotSceneArray:ArrayCollection;
		
		public var recordHotCount:Number;
		public var recordFavoriteCount:Number;
		public var recordRecentCount:Number;
		public var recordFavoriteStateCount:Number;
		public var recordSystemStateCount:Number;
		public var recordRecentOperateSceneStateCount:Number;
		public var recordSystemHotStateCount:Number;
		
		/*------分页相关数据-------*/
		public var pageTools:PageTools;
		public var nums:ArrayCollection;
		public var pageArray:ArrayCollection;
		public var pageCount:Number;
		public var pageIndex:Number = 1;
		private var groupSize:int = 10;
		private var pageSize:int = 18;
		public var hotScenePageIndex:int = 1;
		public var allScenePageIndex:int = 1;
		public var favScenePageIndex:int = 1;
		public var recentScenePageIndex:int = 1;
		public var searchScenePageIndex:int = 1;
		
		public var angle45Scene:Object;
		public var angle90Scene:Object;
		public var angle135Scene:Object;
		
		public var state:int;//1表示从“重新选择场景”
		
		/**
		 * 店铺用户收藏的的场景信息带保存状态
		 * @param scene
		 * 
		 */				
		public function updateStoreFavoriteState(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			favoriteStateSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			systemSceneArray = resetSceneState(favoriteStateSceneArray);
			recordFavoriteStateCount = sceneArr[0].PageCount;
			
			pageTools = new PageTools(recordFavoriteStateCount, groupSize, pageSize, favScenePageIndex);
			dispatch(new PageChangeEvent(PageChangeEvent.UPDATE_PAGE, favScenePageIndex));
		}
		
		/**
		 * 店铺版的系统场景信息带保存和收藏状态
		 * @param scene
		 * 
		 */		
		public function updateStoreSystemSceneState(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			systemStateSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			systemSceneArray = resetSceneState(systemStateSceneArray);;
			recordSystemStateCount = sceneArr[0].PageCount;
			pageTools = new PageTools(recordSystemStateCount, groupSize, pageSize, searchScenePageIndex);
			dispatch(new PageChangeEvent(PageChangeEvent.UPDATE_PAGE, searchScenePageIndex));
		}
		
		/**
		 * 店铺用户最近使用的的场景信息带保存和收藏状态
		 * @param scene
		 * 
		 */		
		
		public function updateStoreRecentOperateSceneState(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			RecentOperateSceneStateArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			systemSceneArray = resetSceneState(RecentOperateSceneStateArray);
			recordRecentOperateSceneStateCount = sceneArr[0].PageCount;
			
			pageTools = new PageTools(recordRecentOperateSceneStateCount, groupSize, pageSize, recentScenePageIndex);
			dispatch(new PageChangeEvent(PageChangeEvent.UPDATE_PAGE, recentScenePageIndex));
		}
		
		/**
		 * 店铺版的热门系统场景信息带保存和收藏状
		 * @param scene
		 * 
		 */				
		public function updateStoreSystemSceneHotState(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			hotSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			systemSceneArray = resetSceneState(hotSceneArray);
			recordHotCount = sceneArr[0].PageCount;
			
			pageTools= new PageTools(recordHotCount, groupSize, pageSize, hotScenePageIndex);	
			dispatch(new PageChangeEvent(PageChangeEvent.UPDATE_PAGE, hotScenePageIndex));
		}
		
		/**
		 *店铺搭配的用户场景 
		 * @param scene
		 * 
		 */			
		public function updateMatchScenes(scene:String):void
		{
			this.scene = scene;
			var t:Array = new Array();
			t.length = 10;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			var array:Array = sceneArr[0].VR_SceneList;
			var newarray:Array = array.concat(t);
			
			matchSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			matcharray = new ArrayCollection(newarray);
			sceneCount = sceneArr[0].PageCount;
			/*if (savedSceneArray != null && sceneCount != 0)
			{
				resetSavedScene();
			}*/
			
		}
		
		/**
		 *用户保存的系统场景 
		 * @param scene
		 * 
		 */			
		public function updateSavedScenes(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			var tempArr:Array = new Array();
			tempArr = sceneArr[0].VR_SceneList;
			var newArr:Array = new Array();
			if (tempArr.length == 0)
			{
				selectedSceneArray = new ArrayCollection();
			}
			else
			{
				selectedSceneArray = new ArrayCollection(tempArr);
				dispatch(new GetStoreSceneDetailEvent(GetStoreSceneDetailEvent.GET_SCENE_DETAIL,tempArr[0].sid));
			}

			/*if (matchSceneArray != null)
			{
				resetSavedScene();
			}	*/		
			
			
			savedSceneCount = String(selectedSceneArray.length);
		}

		private function resetSavedScene():void
		{
			for each(var obj1:Object in matchSceneArray )
			{
				for (var i:int = 0; i < selectedSceneArray.length; i++)
				{
					if (selectedSceneArray.getItemAt(i).sid == obj1.Sourceid)
					{
						selectedSceneArray.getItemAt(i).edited = 1;
					}
					else
						selectedSceneArray.getItemAt(i).edited = 0;
				}
			}
		}
		/**
		 *店铺版的系统场景 
		 * @param scene
		 * 
		 */		
		
		public function updateSystemScenes(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			storeSystemSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);	
		}
		
		/**
		 * 获取店铺前12个热门场景 
		 * @param scene
		 * 
		 */				
		public function updateHotScenes(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			topHotSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			/*recordHotCount = hotSceneArray.length;
			
			var pageTools:PageTools = new PageTools(recordHotCount, 3, 9, pageIndex);
			nums = pageTools.nums;
			pageArray = pageTools.pageArray;
			pageCount = pageTools.pageCount;
			pageIndex = pageTools.pageIndex;*/
		}
		
		/**
		 *店铺收藏场景
		 * @param scene
		 * 
		 */		
		
		public function UpdateStoreFavoriteScene(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			favoriteSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			recordFavoriteCount = sceneArr[0].PageCount;
			
			var pageTools:PageTools = new PageTools(recordFavoriteCount, 3, 9, pageIndex);
			nums = pageTools.nums;
			pageArray = pageTools.pageArray;
			pageCount = pageTools.pageCount;
			pageIndex = pageTools.pageIndex;
		}
		
		/**
		 * 最近使用场景 
		 * @param sceneStr
		 * 
		 */		
		public function UpdateStoreRecentScene(scene:String):void
		{
			this.scene = scene;
			var arr:Array = (JSON.decode(scene) as Array);
			sceneArr = new ArrayCollection(arr);
			recentSceneArray = new ArrayCollection(sceneArr[0].VR_SceneList);
			recordRecentCount = sceneArr[0].PageCount;
			
			var pageTools:PageTools = new PageTools(recordRecentCount, 3, 9, pageIndex);
			nums = pageTools.nums;
			pageArray = pageTools.pageArray;
			pageCount = pageTools.pageCount;
			pageIndex = pageTools.pageIndex;
		}
		
		
		
		public function updateSceneDetail(detailScene:String):void
		{
			var arr:Array = (JSON.decode(detailScene) as Array);
			sceneDetail = arr[0].VR_SceneList[0];
			for each (var angle:Object in sceneDetail.SceneAngleList)
			{
				switch (angle.Jdid)
				{
					case 31:
						angle45Scene = angle;
						break;
					case 30:
						angle135Scene = angle;
						//view.angle135.createScene(angle);
						break;
					case 35:
						angle90Scene = angle;
						//view.angle90.createScene(angle);
						break;
				}
			}
		}
		/**
		 * 判断某个数据源中是否包含待比较的数据，如果包含返回false，如果不包含返回true
		 * 主要用于判断某一数据是否可加入某一数据源中
		 * @param obj：待比较的数据
		 * @return 
		 * 
		 */		
		public function compareObject(obj:Object):Boolean
		{
			for each(var object:Object in selectedSceneArray)
			{
				if (object.sid == obj.sid)
					return false;
			}
			return true;
		}
		
		/**
		 * 源数据与已保存的数据进行对比，重新设置每个系统场景的选择信息
		 * @param sceneArray
		 * @return ArrayCollection
		 * 
		 */		
		private function resetSceneState(sceneArray:ArrayCollection):ArrayCollection
		{
			var sceneCount:int = sceneArray.length;
			for each(var object:Object in selectedSceneArray)
			{
				if (object != null)
				{
					for (var i:int = 0; i < sceneCount; i++)
					{
						if (sceneArray.getItemAt(i).sid == object.sid)
						{
							sceneArray.getItemAt(i).UserSceneState = 1;
						}
					}
				}				
			}
			return sceneArray;
		}
		
		public  function removeSuccessResult(userId:int):void
		{
			var currentUserID:int = userId;
			switch(stateVar)
			{
				case 0:
					dispatch(new GetStoreSystemSceneStateEvent(GetStoreSystemSceneStateEvent.GET_STORE_SYSTEM_SCENE_STATE, 
						                                       currentUserID, cdIdStr, fgIdStr, allScenePageIndex));					
					break;
				case 1:
					dispatch(new GetStoreSystemSceneStateEvent(GetStoreSystemSceneStateEvent.GET_STORE_SYSTEM_SCENE_STATE, 
						                                      currentUserID, cdIdStr, fgIdStr, searchScenePageIndex));
					break;
				case 2:
				{
					dispatch(new GetStoreSystemSceneHotStateEvent(GetStoreSystemSceneHotStateEvent.GET_STORE_SYSTEM_SCENE_HOT_STATE, 
					                                              currentUserID, "","", hotScenePageIndex));
					break;
				}					
				case 3:
				{
					dispatch(new GetStoreFavoriteStateEvent(GetStoreFavoriteStateEvent.GET_STORE_FAVORITE_STATE, 
						                                    currentUserID, favScenePageIndex));
					break;
				}
				case 4:
				{
					dispatch(new GetStoreRecentOperateSceneStateEvent(GetStoreRecentOperateSceneStateEvent.GET_STORE_RECENT_OPERATE_SCENE_STATE, 
						                                               currentUserID, recentScenePageIndex));
					break;
				}					
				default:
					break;
			}
		}
	}
}